import * as api from "@/api/permission"
//引入需要校验的路由
import allRoutes from "@/router/generateRoute"
//引入recursion递归创建用户路由的方法
import recursionRoutes from "@/router/recursionRoutes"
// 引入路由
import router from "@/utils/routerGuard"
// 引入动态路由
import dynamicRoutes from "@/router/dynamicRoutes"

let routes=allRoutes.find(route=>route.name==='home')
export default{
    namespaced:true,
    state: {
        userMenu:[],
        // 面包屑
        crumb:[]
    },
    getters: {},
    mutations: {
        // 设置用户菜单
        SET_USER_MENU(state,payload){
            state.userMenu=[...payload]
            // 使用router.addRoute这个方法动态添加路由
            dynamicRoutes.children=[...dynamicRoutes.children, ...payload]
            router.addRoute(dynamicRoutes)
        },
        // 设置面包屑方法
        SET_CRUMB(state,payload){
            state.crumb=payload
        }
    },
    actions: {
        // 获取远程菜单
        FETCH_MENU_LIST({commit}){
            return api.getMenuListApi().then(res=>{
                // console.log(res.data.menuList)
                let userMenu=recursionRoutes(res.data.menuList,routes.children)
                // console.log(userMenu)
                commit('SET_USER_MENU',userMenu)
            })
        }
    },
    modules: {},
};
